package com.wh.base.sys.role.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wh.base.sys.menu.entity.Menu;
import com.wh.base.sys.menu.service.MenuService;
import com.wh.base.sys.role.entity.Role;
import com.wh.base.sys.role.mapper.RoleMapper;
import com.wh.base.sys.role.service.RoleService;
import com.wh.base.util.StringUtil;

/**
 * <p>
 * 系统角色 服务实现类
 * </p>
 *
 * @author WangHong
 * @since 2022-04-20
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
	
	@Autowired
	private RoleMapper mapper;
	
	@Autowired
	private MenuService menuService;
	
	@Override
	public List<Role> findRoleByUserId(Long userId) {
		return mapper.findRoleByUserId(userId);
	}

	@Override
	public List<String> findRoleFlagByUserId(Long userId) {
		return mapper.findRoleFlagByUserId(userId);
	}

	@Override
	public List<String> findAuthSignByRoleId(Long roleId) {
		//如果是超级管理员则返回所有权限
		if(roleId != null && roleId == 1L) {
			List<String> authSignList = new ArrayList<String>();
			List<Menu> menuList = menuService.list();
			for(Menu m : menuList) {
				if(!StringUtil.isEmpty(m.getAuthSign())) {
					authSignList.add(m.getAuthSign());
				}
			}
			return authSignList;
		}
		
		return mapper.findAuthSignByRoleId(roleId);
	}

	@Override
	public Map<String, List<Menu>> findMenuByUserId(Long userId) {
		Set<Menu> menuSet = new HashSet<>();
		List<Role> roleList = mapper.findRoleByUserId(userId);
		if(roleList != null && roleList.size() > 0) {
			for(Role role : roleList) {
				//超级管理员获取所有菜单
				if(role.getId() != null && role.getId() == 1L) {
					QueryWrapper<Menu> queryWrapper = new QueryWrapper<Menu>();
					queryWrapper.eq("delete_flag", "0");
					queryWrapper.eq("type", "0");
					queryWrapper.orderByAsc("order_num");
					menuSet.addAll(menuService.list(queryWrapper));
					break;
				}else {
					menuSet.addAll(menuService.findMenuListByRoleId(role.getId()));
				}
			}
		}
		
		// 将菜单排序
		List<Menu> setList= new ArrayList<Menu>(menuSet);
		Collections.sort(setList, new Comparator<Menu>() {
			@Override
			public int compare(Menu m1, Menu m2) {
				// TODO Auto-generated method stub
				return m1.getOrderNum().compareTo(m2.getOrderNum());
			}
			
		});
		menuSet = new LinkedHashSet<Menu>(setList);//这里注意使用LinkedHashSet

		
		Map<String, List<Menu>> map = new HashMap<String, List<Menu>>();
		if(menuSet.size() > 0) {
			for(Menu menu : menuSet){
				long parentId = menu.getParentId();
				List<Menu> thisMenuList = map.get(String.valueOf(parentId));
				if(thisMenuList == null){
					thisMenuList = new ArrayList<Menu>();
				}
				thisMenuList.add(menu);
				map.put(String.valueOf(parentId), thisMenuList);
			}
		}
		return map;
	}
	
	
	public List<Map<String, Object>> buildMenuTree(Map<String, List<Menu>> menuMap, String parentId){
		List<Map<String, Object>> nodeList = new ArrayList<Map<String,Object>>();
		List<Menu> rootMenuList = menuMap.get(parentId);
		if(rootMenuList != null && rootMenuList.size() > 0){
			Map<String, Object> tempMap = null;
			for(Menu m : rootMenuList){
				tempMap = new HashMap<String, Object>();
				String menuId = String.valueOf(m.getId());
				tempMap.put("id", menuId);
				tempMap.put("name", m.getName());
				tempMap.put("icon", m.getIcon());
				String menuUrl = m.getUrl();
				tempMap.put("url", menuUrl);
				if(!StringUtil.isEmpty(menuUrl) && menuUrl.indexOf("http") > -1){
					tempMap.put("target", "_blank");
				}else{
					tempMap.put("target", "_self");
				}
				tempMap.put("parentId", m.getParentId());
				List<Menu> childMenuList = menuMap.get(menuId);
				if(childMenuList != null){
					tempMap.put("tags", childMenuList.size());
					tempMap.put("child", buildMenuTree(menuMap, menuId));
				}
				nodeList.add(tempMap);
			}
		}
		return nodeList;
	}

	@Override
	public List<Map<String, Object>> handleRoleList(List<Role> roleList) {
		List<Map<String, Object>> list = new ArrayList<>();
		if(roleList != null && roleList.size() > 0) {
			Map<String, Object> map = null;
			for(Role role : roleList) {
				//过滤掉超级管理员
				if(role.getId() == 1L) {
					continue;
				}
				map = new HashMap<String, Object>();
				map.put("name", role.getName());
				map.put("value", role.getId());
				list.add(map);
			}
		}
		return list;
	}

}
